package com.ht.shortlink.project.mq.producer;

import com.ht.shortlink.project.dto.biz.ShortLinkStatsRecordDTO;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.ht.shortlink.project.common.constant.RedisCacheConstant.DELAY_QUEUE_STATS_KEY;

/**
 *  延迟队列消费短链接统计发送者
 */
@Component
@RequiredArgsConstructor
public class DelayShortLinkStatsProducer {
    private final RedissonClient redissonClient;

    public void send(ShortLinkStatsRecordDTO statsRecord){
        statsRecord.setKeys(UUID.randomUUID().toString());
        RBlockingQueue<ShortLinkStatsRecordDTO> blockingQueue = redissonClient.getBlockingQueue(DELAY_QUEUE_STATS_KEY);
        RDelayedQueue<ShortLinkStatsRecordDTO> delayedQueue = redissonClient.getDelayedQueue(blockingQueue);
        delayedQueue.offer(statsRecord, 5L, TimeUnit.SECONDS);
    }
}
